<?php

/*
Author: dkloz

A function that edits a cateogry. It gets all the users/mods/attributes to be in
the new category and checks the existing ones. Deletes extra, adds new ones
*/

function BL_editCategory($id, $isprivate,$newAttributes, $newModerators, $newUsers)
{
    $oldCategory = BL_getCategory($id);
    $newAttributesArray = array();
    $oldattrnames = array();
    $newUserIDs = array();
    $newMods = array();

	//get all the names
	foreach ($newAttributes as $attribute){
		array_push($newAttributesArray, $attribute->getname());

	}

    foreach ( $oldCategory->GetAttributes() as $attr)
    {
        if (!in_array($attr->getname(), $newAttributesArray))
        {
            //delete attribute attr
            if( ($retval =   DB_deleteAttributeFromCategory($attr, $id)) < 0){
			return BL_EDIT_DELETE_ATTR_FRM_CAT;
            }
        }
    }

	//get all the names
    foreach ( $oldCategory->GetAttributes() as $oldattrs ) {
		array_push($oldattrnames, $oldattrs->getname());
    }

    foreach ($newAttributes as $newAttr)
    {

        if (!in_array($newAttr->getName() , $oldattrnames ))
        {
            if( ($retval = DB_addAttributeToCategory($newAttr, $id)) < 0 ){
			  return BL_EDIT_ADD_ATTR_TO_CAT;
            }
        }
    }

	//get the new users id's
	foreach($newUsers as $newuser){
		array_push($newUserIDs, DB_getUserId($newuser));
	}

    ////////Users
    //DB_getCategoryUsers returns id's
    foreach (DB_getCategoryUsers($id) as $user)
    {
        if (!in_array($user, $newUserIDs))
        {
            //delete user from category

            if( ($retval = DB_deleteUserFromCategory($user, $id)) < 0){
			return BL_EDIT_DELETE_USR_FRM_CAT;

            }
        }
    }

    if(!empty($newUsers[0])){
    
        foreach ($newUserIDs as $newUser)
        {
            if (!in_array($newUser, DB_getCategoryUsers($id)))
            {
                //invite user to category USERID
    
                if (( $retval = DB_addUserToCategory($newUser, $id)) < 0){
    			return BL_EDIT_ADD_USER_TO_CAT;
                }
            }
        }
    }
	//add and delete get ID as a parameter
	//NewModerators is an array with names
	//get category moderators returns ID's
	//get all the id's of the new mods
    $i=0;
	foreach($newModerators as $newmod){
		$newModIDs[$i] = DB_getUserId($newmod);
	   $i++;
    }

    //////Moderators
    foreach (DB_getCategoryModerators($id) as $moderator)
    {
        if (!in_array($moderator, $newModIDs))
        {
            //delete mod from category
            if (( $retval = DB_deleteModeratorFromCategory($moderator, $id)) < 0){
			return BL_EDIT_DELETE_MOD_FROM_CAT;
            }
        }
    }

    if(!empty($newModerators[0])){
        foreach ($newModIDs as $newMod)
        {
            if (!in_array($newMod, DB_getCategoryModerators($id)))
            {
                //invite mod to category MOD ID
                if( ( $retval = DB_addModeratorToCategory($newMod, $id)) < 0){
    			return BL_EDIT_ADD_MOD_TO_CAT;
                }
            }
        }
    }
    
    if(strcmp($isprivate,"public")==0)
        DB_category_setStatus($id, 0);
    else
        DB_category_setStatus($id, 1);
    
}

?>
